package com.ruoyi.framework.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ruoyi.common.utils.ShiroUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * <p>TODO</p>
 *
 * @author jiangxiangjin
 * @version V1.0.0
 * @date 2022/12/23 17:56
 */
@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler  {
    /**
     * 插入操作自动填充
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        metaObject.setValue("createTime", new Date());
        metaObject.setValue("updateTime", new Date());
        metaObject.setValue("createBy", ShiroUtils.getSysUser().getLoginName());
        metaObject.setValue("updateBy", ShiroUtils.getSysUser().getLoginName());
    }

    /**
     * 更新操作自动填充
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        metaObject.setValue("updateTime", new Date());
        metaObject.setValue("updateBy", ShiroUtils.getSysUser().getLoginName());
    }
}
